﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using System.IO;
using COFE.Core;
using NUnit.Framework;
using SevenZip;
using COFE.IO.SZ.Utils;
using COFE.Core.Utils;

namespace COFE.IO.SZ
{
    public class SevenZipDirectoryController : DirectoryControllerBase<SZEntryInfo>
    {
        #region Constructor
        public SevenZipDirectoryController(SevenZipDirectoryLister dirLister)
            : base(dirLister)
        {
            _createFile = (parent, fName) =>
                {
                    var newFile = new ArchiveFileInfo()
                    {
                        FileName = COFEPath.Combine(parent.CurrentItem.FileName, fName),
                        IsDirectory = false,
                        Size = 0
                    };
                    var newEntry = new SZEntryInfo(parent.RootWrapper, newFile);

                    lock (_tempList)
                        _tempList.Add(newEntry);
                   
                    return newEntry;
                };
            _createDirectory = (parent, dName) =>
                {
                    var newDir = new ArchiveFileInfo()
                    {
                        FileName = COFEPath.Combine(parent.CurrentItem.FileName, dName),
                        IsDirectory = true,
                        Size = 0
                    };
                    var newEntry = new SZEntryInfo(parent.RootWrapper, newDir);

                    lock (_tempList)
                        _tempList.Add(newEntry);
                    return newEntry;
                };

            _notifyModified = (item, action) =>
                {
                    if (item.RelativePath == "" &&                  //Is Root
                        !item.RootWrapper.ArchiveFile.IsFileSystem) //FileBasedFS in virtual file system, need update.
                    {
                        (item.RootWrapper.ArchiveFile as IInternalFileInfoExA).UpdateSource();
                    }

                };

            _delete = (item) =>
                {
                    item.RootWrapper.delete(item.RelativeMask);
                    DeleteTempItems(item);                   
                };

        }
        #endregion

        #region Methods

        public IEnumerable<SZEntryInfo> ListTempItems(string rootParseName, string pattern)
        {
            pattern = pattern.TrimStart('\\');
            lock (_tempList)
                foreach (var entryInfo in _tempList)
                    if (COFEPath.GetDirectoryName(pattern) == COFEPath.GetDirectoryName(entryInfo.RelativePath) &&
                        entryInfo.RootWrapper.ArchiveFile.ParseName == rootParseName &&
                        StringUtils.MatchFileMask(entryInfo.RelativePath, pattern))
                        yield return entryInfo;
        }

        public void DeleteTempItems(SZEntryInfo entry)
        {
            DeleteTempItems(entry.ParseName);
        }

        public void DeleteTempItems(string pattern)
        {
            lock (_tempList)
                for (int i = _tempList.Count - 1; i >= 0; i--)
                    if (StringUtils.MatchFileMask(_tempList[i].ParseName, pattern))
                        _tempList.RemoveAt(i);
        }

        #endregion

        #region Data

        static List<SZEntryInfo> _tempList = new List<SZEntryInfo>();

        #endregion

        #region Public Properties

        public List<SZEntryInfo> TempItemList { get { return _tempList; } }

        #endregion


    }
}
